package cn.hzking.gjmall.activitys.second;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.just.agentwebX5.AgentWebX5;
import com.just.agentwebX5.ChromeClientCallbackManager;
import com.just.agentwebX5.DefaultWebClient;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.utils.Logger;
import cn.hzking.gjmall.utils.SPUtils;
import cn.hzking.gjmall.view.SimpleOptionView;
import cn.hzking.gjmall.webview.GjWebInterface;

public class WebGoodDetailActivity extends ColorStatuBarActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.web_ll)
    LinearLayout webLl;
    public static final String NOMAL ="nomal";
    public static final String SEACK ="seack";
    public static final String NYUAN ="nyuan";
    private AgentWebX5 mAgentWebX5;
    private String nomalUrl = "http://gjmall.mxth.com/shop/native/item_detail?shopid=%s&itemId=%s";
    private String seackUrl = "http://gjmall.mxth.com/shop/native/seckll_detail.htm?shopid=%s&msId=%s&itemId=%s";
    private String nyuanUrl = "http://gjmall.mxth.com/shop/native/nselect_detail.htm?shopid=%s&itemId=%s&nsId=%s&skuId=%s";
    private String url = "";
    private String type;//类型 : 普通 nomal , 秒杀: seack N元选: nyuan;
    private String itemId;//商品id
    private String shopId;
    private String msId;//秒杀id
    private String nsId;//n元选id
    private String skuId;//skuid

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_web_good_detail);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        webLl = findViewById(R.id.web_ll);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        itemId = intent.getStringExtra("itemId");
        msId = intent.getStringExtra("msId");
        nsId = intent.getStringExtra("nsId");
        skuId = intent.getStringExtra("skuId");
        shopId = SPUtils.getShopId(this);
        formatUrl();
        initWebView();
    }

    private void formatUrl() {
        if("nomal".equals(type)){//普通商品详情
           url =  String.format(Locale.CHINA,nomalUrl,shopId,itemId);

        }else if("seck".equals(type)){//秒杀商品详情
            String.format(Locale.CHINA,seackUrl,shopId,msId,itemId);

        }else if("nyuan".equals(type)){//n元选商品详情
            String.format(Locale.CHINA,nyuanUrl,shopId,itemId,nsId,skuId);
        }
    }

    public static void startActivity(Context context, String type,String itemId,String msId,String nsId,String skuId){
        Intent intent = new Intent(context,WebGoodDetailActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("itemId",itemId);
        intent.putExtra("msId",msId);
        intent.putExtra("nsId",nsId);
        intent.putExtra("skuId",skuId);
        context.startActivity(intent);

    }
    private void initWebView() {
        String userId = SPUtils.getUserId(this);
        String token = SPUtils.getToken(this);
        Logger.e(token);
        Logger.e(userId);
        mAgentWebX5 = AgentWebX5.with(this)//
                .setAgentWebParent(webLl, new LinearLayout.LayoutParams(-1, -1))//
                .useDefaultIndicator()//
                .defaultProgressBarColor()
                .setReceivedTitleCallback(mCallback)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.DISALLOW)
                .interceptUnkownScheme()
                .additionalHttpHeader("userid", userId)
                .additionalHttpHeader("tokenapp", token)
                .setSecutityType(AgentWebX5.SecurityType.strict)
                .createAgentWeb()//
                .ready()
                .go(null);
        mAgentWebX5.getJsInterfaceHolder().addJavaObject(Gjmall.getJsInterface(), GjWebInterface.create(this));
        mAgentWebX5.getWebCreator().get().getX5WebViewExtension().setScrollBarFadingEnabled(false);
        mAgentWebX5.getLoader().loadUrl(url);
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            Intent intent = new Intent(WebGoodDetailActivity.this, WebGoodDetailActivity.class);
            intent.putExtra("url", s);
            WebGoodDetailActivity.this.startActivity(intent);
            return true;
        }
    };

    private WebChromeClient mWebChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);

        }
    };
    private ChromeClientCallbackManager.ReceivedTitleCallback mCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (simpleTileView != null)
                simpleTileView.setTitle(title);
        }
    };

    @Override
    protected void onDestroy() {
        mAgentWebX5.getWebLifeCycle().onDestroy();
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    protected void onPause() {
        mAgentWebX5.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWebX5.getWebLifeCycle().onResume();
        super.onResume();
    }
}
